package logic

import (
	"context"
	"fmt"
	"person/model/mysql"
	"person/pkg"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PersonLogInLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPersonLogInLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PersonLogInLogic {
	return &PersonLogInLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PersonLogInLogic) PersonLogIn(in *personrpc.PersonLogInRequest) (*personrpc.PersonLogInResponse, error) {
	// todo: add your logic here and delete this line
	var findPerson mysql.Person
	mysql.DB.Debug().Where("phone = ?", in.Phone).Find(&findPerson)
	if findPerson.ID == 0 {
		return nil, fmt.Errorf("该账号未注册,请前往注册")
	}
	if findPerson.Password != pkg.Md5(in.Password) {
		return nil, fmt.Errorf("密码错误")
	}
	return &personrpc.PersonLogInResponse{
		Id: int64(findPerson.ID),
	}, nil
}
